#!/usr/bin/python
# coding: UTF-8 
#@author: rui.xu <mrhaigui@gmail.com>
#Filename:code.py

import web
import time
urls = (
	'/guestbook','guestbook',
	'/.*','index',
	'/clear/?','clear', 
)
app = web.application(urls, globals())
render = web.template.render('templates/')
#class index:
#	def GET(self):
#		name = 'Bob'    
#		return render.index(name)
db = web.database(dbn='sqlite', db='guestbook.db')


class clear:
	def GET(self):
		db.query('delete from comment')
		web.seeother('/guestbook')


class guestbook:
	def GET(self):
		#start=start*10
		input=web.input(page=1)
		page=web.intget(input.page,1)
		#page=string.atoi(page)
		os=page*10
		print page
		comms=db.select('comment')
		yeshu,i=0,0	
		for conment in comms:
			i=i+1
		if i<=10:
			comments=db.select('comment')
		if i>10:
			comments=db.select('comment',limit=10,offset=os-10)
		#yeshu=int(i/10.0)+1
		if i/10.0 > i/10:
			yeshu=int(i/10.0)+1
		elif i/10.0 == i/10:
			yeshu=i/10
		else:
			yeshu=1
		return render.guestbook(comments,i,yeshu)
	def POST(self):
		i = web.input()
		c_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
		if i.author and i.content:
			db.insert('comment', author=i.author, content=i.content,c_time=c_time)
		web.seeother('/guestbook')

class index:
	def GET(self):
		web.seeother('/guestbook')


if __name__ == "__main__": app.run()

